package com.test.test11.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.test.test11.CommonUtils;
import com.test.test11.Config;
import com.test.test11.R;
import com.test.test11.SharedConstants;
import com.test.test11.http.AfterRequest;
import com.test.test11.task.UserLoginTask;

import org.apache.commons.lang3.StringUtils;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "LOGIN_ACTIVITY";

    private EditText et_userName;   //用户名/手机号输入框
    private EditText et_pwd;        //密码框
    private Button bt_login;        //登录按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        // 请求权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            CommonUtils.requestPermission(this, Manifest.permission.INTERNET);
            CommonUtils.requestPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        et_userName = (EditText) findViewById(R.id.et_username);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        bt_login = (Button) findViewById(R.id.bt_login);

        // 读取本地存储的用户信息
        if (SharedConstants.CURRENT_USER != null) {
            et_userName.setText(SharedConstants.CURRENT_USER.getPhoneNumber());
            // 如果跳转到这个界面就让他重新输入密码
        }

        // 点击登录按钮后的操作
        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Config.getNETWORK()==0){
                    // 如果未联网则提示没有网络连接，登录无法进行
                    Toast.makeText(LoginActivity.this, "没有网络连接，请先连接网络", Toast.LENGTH_LONG).show();
                    return;
                }
                // 这里最好做一次trim操作，因为用户经常会多输入空格
                final String phone = et_userName.getText().toString().trim();
                final String pwd = et_pwd.getText().toString().trim();
                if (StringUtils.isBlank(phone) || StringUtils.isBlank(pwd)) {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                UserLoginTask task = new UserLoginTask(new AfterRequest() {
                    @Override
                    public void success() {
                        // 将新用户的信息存储到本地
                        SharedConstants.CURRENT_USER.setPwd(CommonUtils.calMD5(pwd));
                        Config.storeUserInfo(SharedConstants.CURRENT_USER);

                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                        // 跳转到主界面
                        Intent intent = new Intent(LoginActivity.this, DeviceActivity.class);
                        startActivity(intent);
                        finish();  // 添加这句话保证跳转后按返回键不会回来
                    }

                    @Override
                    public void failure(int code, String msg) {
                        if (code == 2002) {
                            Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(LoginActivity.this, "登录失败，请重试", Toast.LENGTH_LONG).show();

                        }
                    }
                });

                // 执行登录请求
                if(CommonUtils.isPhoneNumber(phone)){
                    // 手机号登录
                    task.execute("", CommonUtils.calMD5(pwd), phone);
                }else {
                    // 用户名登录
                    task.execute(phone, CommonUtils.calMD5(pwd), "");
                }
            }
        });
    }

}
